﻿ 
using Microsoft.AspNetCore.SignalR;
using Microsoft.Extensions.Caching.Memory; 

namespace SignalR.Web.Hubs
{

    public class NotifyHub : Hub<INotifyHub>
    {
        private readonly ILogger<NotifyHub> _logger;
        private IMemoryCache _cache;
         
        public NotifyHub(ILogger<NotifyHub> logger, IMemoryCache cache)
        {
            _logger = logger;
            _cache = cache;
        }

        /// <summary>
        /// 给所有客户端发送消息
        /// </summary>
        /// <param name="user">用户</param>
        /// <param name="message">消息</param>
        /// <returns></returns>
        public async Task SendMessage(string user, string message)
        {
            _logger.LogInformation($"SendMessage===== {user} {message}");
            await Clients.All.SendMessage(user, message);
        }
        /// <summary>
        /// 向调用客户端发送消息
        /// </summary>
        /// <param name="message"></param>
        /// <returns></returns>
        public async Task SendMessageCaller(string message)
        {
            _logger.LogInformation($"SendMessageCaller===== {message}");
            await Clients.Caller.ReceiveCaller(message);
        }
         
    } 
}
